import yz_uploader from "components/ui_components/yz_uploader";
//import QrCode from "qrcode-decoder";
import Quagga from "quagga";
export default {
  data() {
    return {
      form: [],

      express: [],

      companyShow: false,

      expressImage: "",
      return_address_list: []
    };
  },
  methods: {
    // change(value){
    //     console.log(value);
    //     this.form.express_code = value.code;
    //     this.express_company_name = value.name;
    // },
    upExpressImage(arr,addressIndex) {
      this.selectIndex = addressIndex;
      this.form[addressIndex].images = arr[0];
    },
    upExpress(file) {
      console.log(file);
      this.decode(file);
    },
    decode(file) {
      Quagga.decodeSingle(
        {
          inputStream: {
            size: 1800 // 这里指定图片的大小，需要自己测试一下
            //   singleChannel: false
          },
          locator: {
            patchSize: "medium",
            halfSample: false
          },
          numOfWorkers: 1,
          decoder: {
            readers: [
              {
                format: "code_128_reader", // ean_reader 这里指定ean条形码，就是国际13位的条形码   code39    code_128
                config: {}
              }
            ]
          },
          // readers: ['code_128_reader'],
          locate: true,
          src: URL.createObjectURL(file)
        },
        result => {
          // let code = result.codeResult.code
          console.log(result, result.codeResult);
          if (result.codeResult && result.codeResult.code) {
            this.form[this.selectIndex].express_sn = result.codeResult.code;
          }
        }
      );
    },
    handleFileChange(evt) {
      const file = evt.target.files[0];
      this.decode(file);
    },

    // getQrUrl(file){
    //   const URi = window.webkitURL.createObjectURL(file) || window.URL.createObjectURL(file);
    //   const url = URi;
    //   const qr = new QrCode();
    //   return qr.decodeFromImage(url);
    // },
    // resolveQR(event){
    //   const result = this.getQrUrl(event);
    //   result.then(res=>{
    //     console.log(res);
    //     if(res.data){
    //       console.log(res.data);
    //       this.expressCode = res.data;
    //     }else {
    //       this.$toast("识别二维码失败");
    //     }
    //   }).catch(err=>{
    //     console.log(JSON.stringify(err));
    //   });
    // },
    companyConfirm(item) {
      this.form[this.selectIndex].express_company_name = item.name;
      this.form[this.selectIndex].express_company_code = item.value;

      this.companyShow = false;
    },
    getExpressInfo(plugin_id) {
      let url = "dispatch.express-list";
      if (plugin_id == 44) {
        url = "plugin.jd-supply.frontend.dispatch.express.express-company";
      }
      $http.get(url, {}, ".").then(
        response => {
          if (response.result == 1) {
            let myData = response.data;
            this.express = myData.express_companies;
          }
        },
        function(response) {
          // error callback
        }
      );
    },
    async getReturnGoods() {
      let { result, msg, data } = await $http.get("refund.detail", { refund_id: this.refund_id }, ".");
      if (result == 0) return this.$toast(msg);
      this.return_address_list = data.return_address_list;
      this.return_address_list.forEach((e,i) => {
        this.form.push({
          "express_sn": "",  //物流编号
          "express_company_code": "", // 物流公司标识
          "order_goods_ids": [], //该快递包裹下订单商品ID数组
          "address": e.province_name + e.city_name + e.district_name + e.street_name + e.address, //发送快递的地址
          "contact": e.contact, //快递收货人名称
          "mobile": e.mobile, //联系方式
          "plane": e.plane,  //非必填
          "images":"",//非必填
          "express_company_name":"",//非必填
        });
        e.refund_order_goods.forEach(g => {
          this.form[i].order_goods_ids.push(g.order_goods_id);
        });
      });
      this.getExpressInfo(data.order.plugin_id);
    },
    onClickCompanyShow(addressIndex){
      this.companyShow = true;
      this.selectIndex = addressIndex;
    },
    submitInfo() {
      $http
        .post(
          "refund.operation.batch-send",
          {
            refund_id: this.refund_id,
            send_info: this.form,
          },
          "正在提交申请"
        )
        .then((response)=> {
          if (response.result == 1) {
            this.$dialog.alert({ message: response.msg }).then(() => {
              this.$router.go(-1);
            });
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        (response)=> {
          // error callback
        });
    }
  },
  activated() {
    this.refund_id = this.$route.params.refund_id;
    this.getReturnGoods();
  },
  components: { yz_uploader }
};
